package com.zhangxin.aiInterview.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.zhangxin.aiInterview.common.BaseResponse;
import com.zhangxin.aiInterview.common.ErrorCode;
import com.zhangxin.aiInterview.common.ResultUtils;
import com.zhangxin.aiInterview.exception.BusinessException;
import com.zhangxin.aiInterview.manager.xfapi.http.ExtractInterviewQuestionApi;
import com.zhangxin.aiInterview.manager.xfapi.http.GenerateExamQuestionApi;
import com.zhangxin.aiInterview.model.dto.community.GetTecInterviewTechStackCountRequest;
import com.zhangxin.aiInterview.model.dto.community.GetTecInterviewTechStackQuestionRequest;
import com.zhangxin.aiInterview.model.dto.community.SubmitInterviewExperienceCommentRequest;
import com.zhangxin.aiInterview.model.dto.community.SubmitInterviewExperienceRequest;
import com.zhangxin.aiInterview.model.entity.*;
import com.zhangxin.aiInterview.model.vo.response.GetCommonInterviewExperienceListResponse;
import com.zhangxin.aiInterview.model.vo.response.GetTecInterviewTechStackCountResponse;
import com.zhangxin.aiInterview.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@RestController("/community")
@Slf4j
public class CommunityController {
    @Resource
    private AiInterviewTechStackService aiInterviewTechStackService;
    @Resource
    private AiQuestionService aiQuestionService;
    @Resource
    private CommunityService communityService;

    /**
     * 根据公司和岗位获取对应的技术面试技术栈数量
     * @return
     */
    @PostMapping("/getTecInterviewTechStackCount")
    public BaseResponse<List<GetTecInterviewTechStackCountResponse>> getTecInterviewTechStackCount(@RequestBody GetTecInterviewTechStackCountRequest getTecInterviewTechStackCountRequest) {
        Long techFieldId = getTecInterviewTechStackCountRequest.getTechFieldId();
        Long positionId = getTecInterviewTechStackCountRequest.getPositionId();
        String company = getTecInterviewTechStackCountRequest.getCompany();
        List<AiInterviewTechStack> list = aiInterviewTechStackService.lambdaQuery()
                .eq(AiInterviewTechStack::getTechFieldId, techFieldId)
                .eq(AiInterviewTechStack::getPositionId, positionId)
                .eq(AiInterviewTechStack::getCompany, company)
                .list();
        List<GetTecInterviewTechStackCountResponse> getTecInterviewTechStackCountResponses = BeanUtil.copyToList(list, GetTecInterviewTechStackCountResponse.class);
        return ResultUtils.success(getTecInterviewTechStackCountResponses);
    }

    /**
     * 根据技术栈获取对应技术栈的面试问题
     * @param getTecInterviewTechStackQuestionRequest
     * @return
     */
    @PostMapping("/getTecInterviewTechStackQuestion")
    public BaseResponse<List<AiQuestion>> getTecInterviewTechStackQuestion(@RequestBody GetTecInterviewTechStackQuestionRequest getTecInterviewTechStackQuestionRequest){
        String company = getTecInterviewTechStackQuestionRequest.getCompany();
        String techStack = getTecInterviewTechStackQuestionRequest.getTechStack();
        AiInterviewTechStack aiInterviewTechStack = aiInterviewTechStackService.lambdaQuery()
                .eq(AiInterviewTechStack::getCompany, company)
                .eq(AiInterviewTechStack::getPositionId, getTecInterviewTechStackQuestionRequest.getPositionId())
                .eq(AiInterviewTechStack::getTechStack, techStack)
                .one();
        Long techStackId = aiInterviewTechStack.getId();
        List<AiQuestion> aiQuestions = aiQuestionService.lambdaQuery()
                .eq(AiQuestion::getTechStackId, techStackId)
                .list();
        return ResultUtils.success(aiQuestions);
    }

    /**
     * 查询所有面经和评论数据
     * @return
     */
    @GetMapping("/get/interviewExperience/list")
    public BaseResponse<List<GetCommonInterviewExperienceListResponse>> getCommonInterviewExperienceList(){
        List<GetCommonInterviewExperienceListResponse> responseList =communityService.getCommonInterviewExperienceList();
        return ResultUtils.success(responseList);
    }

    /**
     * 提交面经
     * @param submitInterviewExperienceRequest
     * @param request
     * @return
     */
    @PostMapping("/submit/InterviewExperience")
    public BaseResponse<Boolean> submitInterviewExperience(@RequestBody SubmitInterviewExperienceRequest submitInterviewExperienceRequest, HttpServletRequest request){
        boolean result = communityService.submitInterviewExperience(submitInterviewExperienceRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 提交面经评论
     * @param submitInterviewExperienceCommentRequest
     * @param request
     * @return
     */
    @PostMapping("/submit/interviewExperience/comment")
    public BaseResponse<Boolean> submitInterviewExperienceComment(@RequestBody SubmitInterviewExperienceCommentRequest submitInterviewExperienceCommentRequest, HttpServletRequest request){
        boolean result = communityService.submitInterviewExperienceComment(submitInterviewExperienceCommentRequest, request);
        return ResultUtils.success(result);
    }
}
